package no.nordicsemi.android.dfuupdate.dfu.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import no.nordicsemi.android.dfuupdate.R;
import no.nordicsemi.android.dfuupdate.bean.TagListData;

public class TagListAdapter extends RecyclerView.Adapter<TagListAdapter.ViewHolder>{
    // 展示数据
    private List<TagListData> mData;
    // 事件回调监听
    private TagListAdapter.OnItemClickListener onItemClickListener;

    /**
     * @param data
     */
    public TagListAdapter(List<TagListData> data) {
        this.mData = data;
    }
    public void updateData(ArrayList<TagListData> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    // ① 定义点击回调接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    // ② 定义一个设置点击监听器的方法
    public void setOnItemClickListener(TagListAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_rv_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // 绑定数据
        holder.name.setText( mData.get(position).getTagName());
        holder.address.setText( mData.get(position).getTagAddress());
        holder.state.setText( ""+mData.get(position).getTagState());
        if(mData.get(position).isUpdate()){
            holder.update.setText( "成功");
        }else{
            holder.update.setText( "失败");
        }

        //③ 对RecyclerView的每一个itemView设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView address;
        TextView state;
        TextView update;
        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.text_name);
            address = (TextView) itemView.findViewById(R.id.text_address);
            state = (TextView) itemView.findViewById(R.id.text_state);
            update = (TextView) itemView.findViewById(R.id.text_update);


        }
    }
}
